using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using Wrath.RuleForge.Common;

namespace Wrath.RuleForge
{
    public partial class RuleSetManager : Form
    {
        ProgressDialog progressDialog;

        public RuleSetManager()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count < 1) return;

            string selectedRuleSet = listView.SelectedItems[0].Text;

            if (MessageBox.Show("Are you sure you want to delete the '" + selectedRuleSet + "' RuleSet?", "Delete RuleSet", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                // Remove directory.
                try
                {
                    Directory.Delete(Globals.FantasyGroundsPaths.RuleSets + "\\" + selectedRuleSet, true);
                }
                catch 
                {
                    MessageBox.Show("There was an error trying to remove the RuleSet directory:\n" + Globals.FantasyGroundsPaths.RuleSets + "\\" + selectedRuleSet + "\n\nYou should remove this folder manually.", "Delete RuleSet", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                // Remove from listview.
                listView.Items.Remove(listView.SelectedItems[0]);

                // Remove from main menu.
                MainForm mainForm = (MainForm)FormBroker.GetForm("main");
                if (mainForm != null)
                {
                    mainForm.RemoveRuleSet(selectedRuleSet);
                }
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count < 1) return;

            string selectedRuleSet = listView.SelectedItems[0].Text;

            StringEntry stringEntry = new StringEntry();
            stringEntry.Text = "Copy RuleSet";
            stringEntry.TextPrompt = "Enter new RuleSet name: ";
            stringEntry.TextEntry = selectedRuleSet;

            if (stringEntry.ShowDialog(this) == DialogResult.OK)
            {
                string newBase = Globals.FantasyGroundsPaths.RuleSets + "\\" + stringEntry.TextEntry;
                string oldBase = Globals.FantasyGroundsPaths.RuleSets + "\\" + selectedRuleSet;

                progressDialog = new ProgressDialog();
                progressDialog.Title = "Copy RuleSet";
                progressDialog.Show();

                progressDialog.CurrentOperation = "Creating directory...";
                progressDialog.Increment(5);
                Directory.CreateDirectory(newBase);

                // Copy RuleSet
                CopyDirectory(oldBase, newBase, 85);

                // Add RuleSet to Main Menu
                progressDialog.CurrentOperation = "Linking RuleSet...";                
                MainForm mainForm = (MainForm)FormBroker.GetForm("main");
                if (mainForm != null)
                {
                    mainForm.AddRuleSet(stringEntry.TextEntry);
                }
                progressDialog.Increment(5);

                // Add RuleSet to ListView
                progressDialog.CurrentOperation = "Refreshing View...";
                ListViewItem newItem = new ListViewItem(stringEntry.TextEntry);
                newItem.ImageKey = "RuleForge.RuleSet";
                listView.Items.Add(newItem);
                progressDialog.Progress = 100;

                progressDialog.Close();
                
            }
        }

        private void CopyDirectory(string oldBase, string newBase, int PercentageOfWhole)
        {
            // Copy files
            DirectoryInfo oldBaseDir = new DirectoryInfo(oldBase);
            FileSystemInfo[] everything = oldBaseDir.GetFileSystemInfos();

            int directoryCount = 0;
            foreach (FileSystemInfo info in everything)
            {
                if ((info.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    directoryCount++;
                }
            }
            
            int directoryIncrement = (int)(PercentageOfWhole / (directoryCount + 1));
            float fileIncrement = ((float)PercentageOfWhole - (directoryIncrement * directoryCount)) / ((float)everything.Length - directoryCount);

            float incrementValue = 0;
            
            foreach (FileSystemInfo info in everything)
            {
                string relativePath = info.FullName.Remove(info.FullName.IndexOf(oldBase), oldBase.Length);

                progressDialog.CurrentOperation = "Copying " + info.Name + "...";

                if ((info.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                {                    
                    Directory.CreateDirectory(newBase + relativePath);
                    CopyDirectory(info.FullName, newBase + relativePath, directoryIncrement);
                }
                else
                {
                    File.Copy(info.FullName, newBase + relativePath);
                    incrementValue += fileIncrement;
                    if (incrementValue > 1.0f)
                    {
                        progressDialog.Increment(1);
                        incrementValue -= 1;
                    }
                }                                
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count < 1) return;
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (listView.SelectedItems.Count < 1) e.Cancel = true;                              
        }

        private void RuleSetManager_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Globals.RuleSets.Count; i++)
            {
                string ruleSet = Globals.RuleSets.GetRuleSet(i);

                ListViewItem newItem = new ListViewItem(ruleSet);
                newItem.ImageKey = "RuleForge.RuleSet";
                listView.Items.Add(newItem);
            }
        }

        private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.LargeIcon;
            smallIconsToolStripMenuItem.Checked = false;
            listToolStripMenuItem.Checked = false;
            detailsToolStripMenuItem.Checked = false;
            tilesToolStripMenuItem.Checked = false;
            largeIconsToolStripMenuItem.Checked = true;
        }

        private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.SmallIcon;
            largeIconsToolStripMenuItem.Checked = false;
            listToolStripMenuItem.Checked = false;
            detailsToolStripMenuItem.Checked = false;
            tilesToolStripMenuItem.Checked = false;
            smallIconsToolStripMenuItem.Checked = true;
        }

        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.List;
            smallIconsToolStripMenuItem.Checked = false;
            largeIconsToolStripMenuItem.Checked = false;
            detailsToolStripMenuItem.Checked = false;
            tilesToolStripMenuItem.Checked = false;
            listToolStripMenuItem.Checked = true;
        }

        private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.Details;
            smallIconsToolStripMenuItem.Checked = false;
            listToolStripMenuItem.Checked = false;
            largeIconsToolStripMenuItem.Checked = false;
            tilesToolStripMenuItem.Checked = false;
            detailsToolStripMenuItem.Checked = true;
        }

        private void tilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.Tile;
            smallIconsToolStripMenuItem.Checked = false;
            listToolStripMenuItem.Checked = false;
            detailsToolStripMenuItem.Checked = false;
            largeIconsToolStripMenuItem.Checked = false;
            tilesToolStripMenuItem.Checked = true;
        }

        private void noneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.Sorting = SortOrder.None;
            noneToolStripMenuItem.Checked = true;
            ascendingToolStripMenuItem.Checked = false;
            descendingToolStripMenuItem.Checked = false;
        }

        private void ascendingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.Sorting = SortOrder.Ascending;
            noneToolStripMenuItem.Checked = false;
            ascendingToolStripMenuItem.Checked = true;
            descendingToolStripMenuItem.Checked = false;
        }

        private void descendingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.Sorting = SortOrder.Descending;
            noneToolStripMenuItem.Checked = false;
            ascendingToolStripMenuItem.Checked = false;
            descendingToolStripMenuItem.Checked = true;
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count < 1) return;

        }
    }
}